package com.springboot.mybatis;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author sunyt
 * @date 2020/7/16 13:30
 */
public class Test {
    public static void main(String[] args) throws ParseException {

        String time="20200625";
//        System.out.println(addDate(time));

        String dateFlag = getHoliday(time);
        while ("1".equals(dateFlag) || "2".equals(dateFlag)) {
            String addDate = addDate(time);
            dateFlag =getHoliday(addDate);
            time = addDate;
            continue;
//            System.err.println("今天是法定节假日");
        }
            System.out.println(time+"是工作日");

    }

    /**
     *
     * 方法描述：方法描述：获取节假日 访问接口，根据返回值判断当前日期是否为工作日，
     * 返回结果：检查具体日期是否为节假日，工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2；
     *
     */
    public static  String  getHoliday(String  time) {
        String dc = "http://tool.bitefu.net/jiari/?d=";
        String  httpUrl=new StringBuffer().append(dc).append(time).toString();
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            return result;
        }finally {

        }
        return result;
    }

    public static String addDate(String classDateString) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyyMMdd"); //定义日期格式化的格式
//        String classDateString = "2015-08-12"//需要加减的字符串型日期
        Date classDate = format.parse(classDateString);//把字符串转化成指定格式的日期
        Calendar calendar = Calendar.getInstance(); //使用Calendar日历类对日期进行加减
        calendar.setTime(classDate);
        calendar.add(Calendar.DAY_OF_MONTH, + 1);
        classDate = calendar.getTime();//获取加减以后的Date类型日期
        return format.format(classDate);
    }

}
